package com.example.parallelandroid;

import static com.mob.MobSDK.getContext;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.Switch;

import java.util.Calendar;
import java.util.GregorianCalendar;

public class ClockActivity extends AppCompatActivity {
    private SharedPreferences dao,zheng ;
    private Switch sitSwitch,lateSwitch;
    private Toolbar toolbar;
    private SharedPreferences sharedPreferences;
    private SharedPreferences.Editor editor;
    private boolean sit,late;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_clock);
        sitSwitch = (Switch) findViewById(R.id.sitlong);
        lateSwitch = (Switch) findViewById(R.id.late);
        toolbar = findViewById(R.id.clocktoolbar);

        // 为ToolBar添加返回键
        setSupportActionBar(toolbar);
        getSupportActionBar().setTitle("");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true); // 添加默认的返回图标
        getSupportActionBar().setHomeButtonEnabled(true); // 设置返回键可用
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish(); // 返回
            }
        });

        // 获取SharedPreferences
        dao = getContext().getSharedPreferences("isStartDao", Context.MODE_PRIVATE);
        zheng = this.getSharedPreferences("isStartZheng",Context.MODE_PRIVATE);
        //与提醒有关的sp
        sharedPreferences = getSharedPreferences("Clock", 0);
        sit = sharedPreferences.getBoolean("sitSwitch",false);
        late = sharedPreferences.getBoolean("lateSwitch",false);
        //设置开关默认状态
        if (sit){
            sitSwitch.setChecked(true);
        }else{

        }
        if (late){
            lateSwitch.setChecked(true);
        }



        sitSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                editor = sharedPreferences.edit();
                editor.putBoolean("sitSwitch",isChecked);
                editor.commit();
                if (isChecked){
                    //如果开启了久坐提醒，则新建一个时间
                    GregorianCalendar calendar = new GregorianCalendar();
                    int hour = calendar.get(Calendar.HOUR_OF_DAY);
                    editor = sharedPreferences.edit();
                    editor.putInt("hour",hour);
                    editor.commit();
                    Constant.logger.info("久坐提醒: 开启");
                }
            }
        });
        lateSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
                editor = sharedPreferences.edit();
                editor.putBoolean("lateSwitch",isChecked);
                editor.commit();
                if (isChecked){
                    Constant.logger.info("熬夜提醒: 开启");
                }
            }
        });
    }

}